﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Text;
using System.Data.OleDb;

public partial class upload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (CheckLogined())
        {
            bool flg = new sql().CheckPower(Session["su_no"].ToString(), "81");
            if (!flg)
                Response.Write("no");
            try
            {
                // 获得程序路径
                string filePath = System.Configuration.ConfigurationManager.AppSettings["Upload"];
                //找到目标文件对象
                HttpPostedFile uploadFile = Request.Files["Fdata"];

                // 如果有文件, 则保存到一个地址
                if (uploadFile.ContentLength > 0)
                {
                    //获取要保存的文件信息
                    FileInfo file = new FileInfo(uploadFile.FileName);

                    //获得文件扩展名
                    string fileNameExt = file.Extension;

                    //新文件名称
                    string fileName = GetFileName() + fileNameExt;

                    if (".xls".Equals(fileNameExt) || ".xlsx".Equals(fileNameExt))
                    {
                        //保存的文件路径
                        filePath = Server.MapPath(filePath + "dangan/");
                        if (!Directory.Exists(filePath))
                            Directory.CreateDirectory(filePath);

                        uploadFile.SaveAs(filePath + fileName);

                        DataTable dt = GetExcelData(filePath + fileName);
                        int ret = 0;
                        if(dt != null && dt.Rows.Count>0)
                            ret = new sql().SaveArchives(Session["su_no"].ToString(), Session["su_login"].ToString(), Request.UserHostAddress, dt);

                        Response.Write(ret);
                    }
                    else
                        Response.Write(-2);
                }
            }
            catch
            {
                Response.Write(-1);
            }
        }
        else
            Response.Write("out");
        Response.End();
    }

    private bool CheckLogined()
    {
        bool res = false;
        if (Session["su_no"] != null)
        {
            try
            {
                int su_no = Convert.ToInt32(Session["su_no"]);
                Session["su_no"] = su_no;
                string su_login = Convert.ToString(Session["su_login"]);
                Session["su_login"] = su_login;
                string su_power = Convert.ToString(Session["su_power"]);
                Session["su_power"] = su_power;
                res = true;
            }
            catch (Exception e)
            {
                res = false;
            }
        }
        return (res);
    }

    private string GetFileName()
    {
        Random rd = new Random();
        StringBuilder sb = new StringBuilder();
        sb.Append(DateTime.Now.ToString("yyyyMMddHHmmssff"));
        sb.Append(rd.Next(0, 999999).ToString());
        return sb.ToString();
    }

    private DataTable GetExcelData(string filepath)
    {
        string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";
        OleDbConnection ExcelConn = new OleDbConnection(strCon);
        try
        {
            ExcelConn.Open();
            DataTable sheetNames = ExcelConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
            string strCom = string.Format("SELECT * FROM [" + sheetNames.Rows[0]["TABLE_NAME"].ToString() + "]");
            OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, ExcelConn);
            DataTable dt = new DataTable();
            myCommand.Fill(dt);
            ExcelConn.Close();
            return dt;
        }
        catch (Exception ex)
        {
            ExcelConn.Close();
            throw ex;
        }
    }
}